class Solution(object):
    def calculate(self, s):
        """
        :type s: str
        :rtype: int
        """

        stack = []
        op = '+'
        i = 0
        while i < len(s):

            if s[i].isdigit():

                num = int(s[i])
                i += 1
                while i < len(s) and s[i].isdigit():
                    num = num * 10 + int(s[i])
                    i += 1

                if op == '+':
                    stack.append(num)
                elif op == '-':
                    stack.append(-num)
                elif op == '*':
                    stack.append(stack.pop() * num)
                elif op == '/':
                    pop_num = stack.pop()
                    if pop_num < 0:
                        stack.append(-int((-pop_num) / num))
                    else:
                        stack.append(int(pop_num / num))
                # print(int(-3/2))
                # print(stack)
                continue
            elif s[i] == ' ':
                pass
            else:
                op = s[i]
            i += 1

        return sum(stack)